package com.zy.web.controller;

import java.util.List;
import java.util.Arrays;

import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.zy.common.annotation.RepeatSubmit;
import com.zy.common.annotation.Log;
import com.zy.common.core.controller.BaseController;
import com.zy.common.core.domain.PageQuery;
import com.zy.common.core.domain.R;
import com.zy.common.core.validate.AddGroup;
import com.zy.common.core.validate.EditGroup;
import com.zy.common.enums.BusinessType;
import com.zy.common.utils.poi.ExcelUtil;
import com.zy.web.domain.vo.ZyResultVo;
import com.zy.web.domain.bo.ZyResultBo;
import com.zy.web.service.IZyResultService;
import com.zy.common.core.page.TableDataInfo;

/**
 * 结果
 *
 * @author zy
 * @date 2024-02-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/web/result")
public class ZyResultController extends BaseController {

    private final IZyResultService iZyResultService;

    /**
     * 查询结果列表
     */
//    @SaCheckPermission("web:result:list")
    @GetMapping("/list")
    public TableDataInfo<ZyResultVo> list(ZyResultBo bo, PageQuery pageQuery) {
        return iZyResultService.queryPageList(bo, pageQuery);
    }
    /**
     * 导入结果列表
     */
    @PostMapping("/import")
    @ResponseBody
    public R<Long> importExcel(@RequestExcel List<ZyResultBo> list, BindingResult bindingResult) {
        return R.ok(list.stream().map(iZyResultService::insertByBo).filter(v -> v == true).count());
    }

    /**
     * 导出结果列表
     */
//    @SaCheckPermission("web:result:export")
    @Log(title = "结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ZyResultBo bo, HttpServletResponse response) {
        List<ZyResultVo> list = iZyResultService.queryList(bo);
        ExcelUtil.exportExcel(list, "结果", ZyResultVo.class, response);
    }

    /**
     * 获取结果详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("web:result:query")
    @GetMapping("/{id}")
    public R<ZyResultVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iZyResultService.queryById(id));
    }

    /**
     * 新增结果
     */
    @SaCheckPermission("web:result:add")
    @Log(title = "结果", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ZyResultBo bo) {
        return toAjax(iZyResultService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改结果
     */
    @SaCheckPermission("web:result:edit")
    @Log(title = "结果", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ZyResultBo bo) {
        return toAjax(iZyResultService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除结果
     *
     * @param ids 主键串
     */
    @SaCheckPermission("web:result:remove")
    @Log(title = "结果", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iZyResultService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

}
